//
//  CTPPhotosCollectionView.m
//  LittleNotes
//
//  Created by 李贤立 on 16/5/23.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "CTPPhotosCollectionView.h"
#import "PhotoPickerCell.h"
#import "HMImagePickerController.h"
@interface CTPPhotosCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate,HMImagePickerControllerDelegate>
/**
 *  存储选择图片的数组
 */
@property(nonatomic,strong) NSMutableArray<UIImage *> *images;
@property(nonatomic,strong) NSMutableArray *selectedAssets;
/**
 *  展示图片的collectionView
 */
@property(nonatomic, weak) IBOutlet UICollectionView *collectionView;
/**
 *  layout
 */
@property(nonatomic,strong) IBOutlet UICollectionViewFlowLayout *layout;
/**
 *  相片选择器
 */
@property(nonatomic,strong) HMImagePickerController *picker;
@end
@implementation CTPPhotosCollectionView
+ (instancetype)collectionView {
    return [[[NSBundle mainBundle] loadNibNamed:@"CTPPhotosCollectionView" owner:nil options:nil] lastObject];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupCollection];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupCollection];
    }
    return self;
}
- (void)awakeFromNib {
    [self setupCollection];
}
- (void)setupCollection {
    // 设置背景
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // 指定数据源额代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    // 设置layout
    self.layout.itemSize = CGSizeMake(80, 80);
    // 注册
    [self.collectionView registerClass:[PhotoPickerCell class] forCellWithReuseIdentifier:@"photos"];
    // 设置基本间距
    self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
}
- (void)didClickAddBtn {
    self.picker = [[HMImagePickerController alloc] initWithSelectedAssets:self.selectedAssets];
    self.picker.pickerDelegate = self;
    // 设置目标图片尺寸
    self.picker.targetSize = CGSizeMake(600, 600);
    // 设置最大选择照片的数量
    self.picker.maxPickerCount = MaxPhotosCount;
    // 进入选择照片控制器
    if (self.jumpToVCBlock) {
        self.jumpToVCBlock(self.picker);
    }
}
- (void)didClickDeleteBtn:(PhotoPickerCell *)cell {
    // 移除对象
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    [self.images removeObjectAtIndex:indexPath.item];
    [self.selectedAssets removeObjectAtIndex:indexPath.item];
    // 刷新选择
    [self.collectionView reloadData];
}
#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 返回比选择的照片多一个
    CGFloat count = self.images.count != 0 ? self.images.count + 1 : 1;
    if (self.images.count == 0) {
        count = 1;
    } else if (self.images.count < MaxPhotosCount) {
        count = self.images.count + 1;
    } else {
        count = MaxPhotosCount;
    }
    return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoPickerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photos" forIndexPath:indexPath];
    // 传递图片
    if (indexPath.item < self.images.count) {
        cell.image = self.images[indexPath.item];
    } else {
        [cell showAddBtn];
    }
    // 删除选择的图片
    __weak typeof(cell) weakCell = cell;
    [cell setDeleteImagesBlock:^{
        [self didClickDeleteBtn:weakCell];
    }];
    // 选择图片
    [cell setPickerImagesBlock:^{
        [self didClickAddBtn];
    }];
    return cell;
}
#pragma mark - 实现黑马选择器的代理
- (void)imagePickerController:(HMImagePickerController *)picker didFinishSelectedImages:(NSArray<UIImage *> *)images selectedAssets:(NSArray<PHAsset *> *)selectedAssets {
    // 获得的图片
    [self.images addObjectsFromArray:images];
    self.images = [self newArrayFromOldArray:self.images];
    // 已经选择的图片
    [self.selectedAssets addObjectsFromArray:selectedAssets];
    self.selectedAssets = [self newArrayFromOldArray:self.selectedAssets];
    // 退出照片选择器
    [self.picker dismissViewControllerAnimated:YES completion:nil];

    // 刷新显示
    [self.collectionView reloadData];
}
// 遍历旧数组,删除相同的元素,返回新数组
- (NSMutableArray *)newArrayFromOldArray:(NSMutableArray *)array {
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        for (NSInteger count; count < array.count; count++) {
            if (obj == array[count]) {
                [array removeObject:array[count]];
            }
        }
    }];
    return array;
}
#pragma mark -懒加载
- (NSMutableArray<UIImage *> *)images {
    if (_images == nil) {
        _images = [NSMutableArray array];
    }
    return _images;
}
- (NSMutableArray *)selectedAssets {
    if (_selectedAssets == nil) {
        _selectedAssets = [NSMutableArray array];
    }
    return _selectedAssets;
}
@end
